#ifndef __interface_h__
#define __interface_h__

#include <functional>
#include <string>
#include <unordered_map>

enum class StepMainStatus { PENDING, RUNNING, COMPLETED, FAILED };

class IWorkflowNotify {
 public:
  virtual ~IWorkflowNotify() = default;
  virtual void onStepFailed(const std::string &step_name,
                            const std::string &reason) = 0;
  virtual std::string getWorkflowName() const = 0;
};

class IDataCollectionSupport {
 public:
  virtual ~IDataCollectionSupport() = default;
  virtual void prepareForCollection() const = 0;
};

class Step {
 public:
  Step(const std::string &name, IWorkflowNotify *notify,
       unsigned int initial_status)
      : name_(name), notify_(notify), current_status_(initial_status) {
    if (!notify) {
      throw std::invalid_argument("Workflow notify cannot be null");
    }
  }

  virtual ~Step() = default;

  std::string getName() const { return name_; }
  bool isCompleted() const { return main_status_ == StepMainStatus::COMPLETED; }
  bool isFailed() const { return main_status_ == StepMainStatus::FAILED; }
  std::string getCurrentStatusString() const {
    return statusToString(current_status_);
  }

  bool execute(int &progress);

 protected:
  std::string name_;
  IWorkflowNotify *notify_;
  StepMainStatus main_status_ = StepMainStatus::PENDING;
  unsigned int current_status_;  // 用整数存储当前状态
  std::unordered_map<unsigned int, std::function<bool(unsigned int &, int &)>>
      status_handlers_;

  // 子类必须实现的纯虚函数
  virtual std::string statusToString(unsigned int status) const = 0;
  virtual bool isCompleteStatus(unsigned int status) const = 0;
  virtual unsigned int getFailedStatus() const = 0;
};

#endif